$(function () {
  let layer = layui.layer;
  function getComment() {
    $.ajax({
      url: 'admin/comments',
      success: function (res) {
        let htmlStr = template('commentTpl', res);
        $('#tb').html(htmlStr);
      },
    });
  }
  getComment();
  //补0函数
  //   let addZero = (n) => (n < 10 ? '0' + n : n);
  function addZero(n) {
    return n < 10 ? '0' + n : n;
  }
  //时间过滤器
  template.defaults.imports.dateFormat = function (cdate) {
    let date = new Date(cdate);

    let y = addZero(date.getFullYear());
    let M = addZero(date.getMonth() + 1);
    let d = addZero(date.getDate());
    let h = addZero(date.getHours());
    let m = addZero(date.getMinutes());
    let s = addZero(date.getMinutes());

    return `${y}-${M}-${d} ${h}:${m}:${s}`;
  };
  //点击事件 删除评论
  $('body').on('click', '.delBtn', function () {
    // console.log(1);
    //自定义属性获取ID
    let id = $(this).attr('data-id');
    //发送Ajax删除评论

    layer.confirm('手滑? 不再考虑一下????', { icon: 3, title: '提示' }, function (index) {
      $.ajax({
        url: 'admin/comments/' + id,
        type: 'DELETE',
        success: function (res) {
          if (res.status !== 0) {
            return layer.msg(res.message);
          }
          layer.msg(res.message);
          getComment();
        },
      });
      layer.close(index);
    });
  });
});
